<template>
  <div class="dashboard-container">
    <div class="app-container">
      <PageTools>
        <el-button type="success" size="small" @click="$router.push('/import?type=employee')">导入</el-button>
        <el-button
          type="danger"
          size="small"
          :loading="downloadLoading"
          @click="handleExport"
        >导出</el-button>
        <el-button type="primary" size="small" class="el-icon-plus" @click="add">新增员工</el-button>
      </PageTools>
      <el-card>
        <el-table border :data="list">
          <el-table-column label="序号" align="center" sortable type="index" :index="indexMethod" />
          <el-table-column label="姓名" align="center" prop="username" />
          <el-table-column label="工号" align="center" sortable prop="workNumber" />
          <el-table-column label="聘用形式" align="center" prop="formOfEmployment" :formatter="formatterEmp" />
          <el-table-column label="部门" align="center" prop="departmentName" />
          <el-table-column label="入职时间" align="center" ortable>
            <template v-slot="{row}">
              {{ row.timeOfEntry | formatTime }}
            </template>
          </el-table-column>
          <el-table-column label="账户状态" align="center" prop="enableState">
            <template v-slot="{row}">
              <el-switch
                v-model="row.enableState"
                active-color="#13ce66"
                :active-value="1"
                :inactive-value="0"
                inactive-color="#ff4949"
              />
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" width="280">
            <template v-slot="{row}">
              <el-button type="text" size="small">查看</el-button>
              <el-button type="text" size="small">转正</el-button>
              <el-button type="text" size="small">调岗</el-button>
              <el-button type="text" size="small">离职</el-button>
              <el-button type="text" size="small">角色</el-button>
              <el-button type="text" size="small" @click="del(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <el-pagination
          :current-page="page"
          :page-sizes="[8, 16, 24, 32]"
          background
          :page-size="size"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </el-card>
      <!-- 添加员工组件 -->
      <AddEmployee :show-dialog.sync="showDialog" @addEmp="addEmp" />
    </div>
  </div>
</template>

<script>
import handleStyle from '@/api/constant/employees'
import { getAllEmpolyees, delEmployee } from '@/api/employees'
import AddEmployee from './components/AddEmployee.vue'

import moment from 'moment'
export default {
  name: 'Employees',
  components: {
    AddEmployee
  },
  data() {
    return {
      page: 1,
      size: 8,
      list: [],
      total: 0,
      showDialog: false,
      downloadLoading: false
    }
  },
  created() {
    this.getAllEmpolyeesList()
  },
  methods: {
    // 1.渲染数据
    async getAllEmpolyeesList() {
      const res = await getAllEmpolyees(this.page, this.size)
      // console.log(res)
      this.list = res.rows
      this.total = res.total
    },
    // 2. 点击数据显示的条数
    async handleSizeChange(val) {
      this.page = 1
      this.size = val
      await getAllEmpolyees(this.page, this.size)
      this.getAllEmpolyeesList()
    },
    // 3.点击当前页码
    async handleCurrentChange(val) {
      this.page = val
      await getAllEmpolyees(this.page, this.size)
      this.getAllEmpolyeesList()
    },
    // 4. 处理序号
    indexMethod(index) {
      return index + 1 + (this.page - 1) * this.size
    },
    // 5.处理 聘用形式 和 账户状态 以及 时间
    formatterEmp(row, column, cellValue, index) {
      const obj = handleStyle.hireType.find(item => item.id === cellValue)
      return obj ? obj.value : ''
    },
    // 6.点击删除某个员工
    async del(id) {
      try {
        await this.$confirm('你确定删除此员工吗?', '温馨提示')
      } catch {
        return
      }
      await delEmployee(id)
      this.$message.success('删除员工成功')
      // 重新渲染
      this.getAllEmpolyeesList()
    },
    // 7.添加员工
    add() {
      this.showDialog = true
    },
    // 8. 子组件通知父组件添加成功,且跳转到添加到的那个页面
    addEmp() {
      this.showDialog = false
      this.total++
      this.page = Math.ceil(this.total / this.size)
      this.getAllEmpolyeesList()
    },
    // 9.导出处理
    // 处理导出excel
    handleExport() {
      this.downloadLoading = true
      import('@/vendor/Export2Excel').then(async excel => {
        // header: 表头
        // data: 数据
        // filename: 文件名
        // autoWidth: excel是否自动宽度
        // bookType： 后缀
        const { rows } = await getAllEmpolyees(1, this.total)
        console.log(rows)
        const userRelations = {
          '手机号': 'mobile',
          '姓名': 'username',
          '入职日期': 'timeOfEntry',
          '聘用形式': 'formOfEmployment',
          '转正日期': 'correctionTime',
          '工号': 'workNumber',
          '部门': 'departmentName'
        }
        const header = Object.keys(userRelations)
        const data = rows.map(item => {
          return header.map(k => {
            const value = item[userRelations[k]]
            if (value && (k === '入职日期' || k === '转正日期')) {
              return moment(value).format('YYYY-MM-DD')
            }
            if (k === '聘用形式') {
              const obj = handleStyle.hireType.find(item => item.id === value)
              return obj ? obj.value : ''
            }
            return value
          })
        })
        // console.log(data)

        excel.export_json_to_excel({
          header,
          data,
          filename: '员工数据',
          autoWidth: true,
          bookType: 'xlsx'
        })
        this.downloadLoading = false
      })
    }
  }
}
</script>

<style>

</style>
